﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MuzoBlog.Core.BussinessLogic.Blog.Entities;
using MuzoBlog.Core.Helpers;
using MuzoBlog.Core.BussinessLogic.Settings;

namespace MuzoBlog.Core.BussinessLogic.Blog
{
    public class BlogManager
    {
        public static void Create(string userName, string password, List<Setting> defaultSettings)
        {
            BlogData blogData = new BlogData();
            string encryptionKey = RandomHelper.GeneratePassword(16);

            blogData.Info.Username = userName;
            blogData.Info.Password = EncryptionHelper.Encrypt(encryptionKey, password);

            #region FillDefaultBlogEntity

            Guid postID = Guid.NewGuid();

            blogData.Posts.Add(new Post()
            {
                PostID = postID,
                ParentPostID = Guid.Empty,
                PostDate = DateTime.Now,
                PostDateGMT = DateTime.UtcNow,
                Slug = "welcome",
                Status = PostStatus.Published,
                Summary = "Welcome summary",
                Type = PostType.Post,
                Title = "Welcome to the muz0",
                Content = "Welcome text goes here",
            });

            blogData.Comments.Add(new Comment()
            {
                CommentID = Guid.NewGuid(),
                PostID = postID,
                Author = "Author name",
                AuthorComment = "Comment goes here!",
                AuthorEmail = "omer@muz0.com",
                AuthorIP = "127.0.0.1",
                AuthorUrl = "http://mysite.com",
                CommentDate = DateTime.Now,
                CommentDateGMT = DateTime.UtcNow,
                Type = CommentType.UserComment
            });

            // Do not delete this region. This is a initial setting region
            
            #region InitialSettings

            // EncryptionKey
            blogData.Settings.Add(new Setting()
            {
                Name = "EncryptionKey",
                Value = encryptionKey,
                Description = "Encryption key is using for password and file encryption"
            });

            blogData.Settings.AddRange(defaultSettings);

            #endregion

            #endregion

            MuzoApp.Db = blogData;
            MuzoApp.Save();
        }
    }
}